package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Resource
    private SpecParamService specParamService;

    @Resource
    private SpecGroupService specGroupService;

    /**
     * 根据商品分类id，查询规格组的集合
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupsByCategoryId(@RequestParam("id") Long id){

        List<SpecGroupDTO> specGroupDTOList = specGroupService.queryGroupsByCategoryId(id);
        return ResponseEntity.ok(specGroupDTOList);
    }

    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        List<SpecParamDTO> specParamDTOList= specParamService.querySpecParams(categoryId,groupId,searching);
        return ResponseEntity.ok(specParamDTOList);
    }

    /**
     * 根据分类id查询规格组集合及组内参数
     * @param id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecListByCategoryId(@RequestParam("id") Long id){

        List<SpecGroupDTO> specGroupDTOList = specGroupService.querySpecListByCategoryId(id);
        return ResponseEntity.ok(specGroupDTOList);
    }

    /**
     * 新增规格组
     * @param specGroupDTO 规格组信息
     * @return 201
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.saveOrUpdateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO 规格组信息
     * @return 201
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.saveOrUpdateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO 规格组参数信息
     * @return 201
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.saveOrUpdateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO 规格组参数信息
     * @return 201
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.saveOrUpdateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
